package com.wzt.lianfirecontrol.adapter.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.wzt.lianfirecontrol.R;
import com.wzt.lianfirecontrol.adapter.BaseRecyclerAdapter;
import com.wzt.lianfirecontrol.bean.recode.message.MessageTypeModel;
import com.wzt.lianfirecontrol.utils.GildeUtils;
import com.wzt.lianfirecontrol.utils.StringUtils;

public class HomeMessageAdapter extends BaseRecyclerAdapter<MessageTypeModel> {

    private Context context;

    public HomeMessageAdapter(Context context) {
        this.context = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreate(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_home_message, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBind(RecyclerView.ViewHolder viewHolder, int RealPosition, MessageTypeModel data) {
        if (viewHolder instanceof MyViewHolder){
            setItemData((MyViewHolder)viewHolder, data);
        }
    }

    private class MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView iv_home_message_img;
        private TextView tv_home_message_title;
        private TextView tv_home_no_message;
        private TextView tv_home_message_num;
        public MyViewHolder(View itemView) {
            super(itemView);
            initItemView(this, itemView);
        }
    }

    private void initItemView(MyViewHolder myViewHolder, View itemView) {
        myViewHolder.iv_home_message_img = itemView.findViewById(R.id.iv_home_message_img);
        myViewHolder.tv_home_message_title = itemView.findViewById(R.id.tv_home_message_title);
        myViewHolder.tv_home_no_message = itemView.findViewById(R.id.tv_home_no_message);
        myViewHolder.tv_home_message_num = itemView.findViewById(R.id.tv_home_message_num);
    }

    private void setItemData(MyViewHolder viewHolder, MessageTypeModel model) {
        if (StringUtils.isEmpty(model.getUrl())){
            viewHolder.iv_home_message_img.setVisibility(View.GONE);
        }else{
            viewHolder.iv_home_message_img.setVisibility(View.VISIBLE);
            GildeUtils.loadImage(context, viewHolder.iv_home_message_img, model.getUrl());
        }

        if (StringUtils.isEmpty(model.getMsgTypeName())){
            viewHolder.tv_home_message_title.setVisibility(View.GONE);
        }else {
            viewHolder.tv_home_message_title.setVisibility(View.VISIBLE);
            viewHolder.tv_home_message_title.setText(model.getMsgTypeName());
        }

        if (!StringUtils.isEmpty(model.getMsgNum()) && !"0".equals(model.getMsgNum())){
            viewHolder.tv_home_message_num.setVisibility(View.VISIBLE);
            try {
                if (Integer.parseInt(model.getMsgNum()) > 99) {
                    viewHolder.tv_home_message_num.setText("99+");
                } else {
                    viewHolder.tv_home_message_num.setText(model.getMsgNum());
                }
            }catch (Exception e){}
            viewHolder.tv_home_no_message.setVisibility(View.GONE);
        }else {
            viewHolder.tv_home_message_num.setVisibility(View.GONE);
            viewHolder.tv_home_no_message.setVisibility(View.VISIBLE);
        }

    }
}
